#!/bin/bash

# -------------------------------------------------------------------------- #
# Copyright 2002-2023, OpenNebula Project, OpenNebula Systems                #
#                                                                            #
# Licensed under the Apache License, Version 2.0 (the "License"); you may    #
# not use this file except in compliance with the License. You may obtain    #
# a copy of the License at                                                   #
#                                                                            #
# http://www.apache.org/licenses/LICENSE-2.0                                 #
#                                                                            #
# Unless required by applicable law or agreed to in writing, software        #
# distributed under the License is distributed on an "AS IS" BASIS,          #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
# See the License for the specific language governing permissions and        #
# limitations under the License.                                             #
#--------------------------------------------------------------------------- #

# MV <hostA:system_ds/disk.i|hostB:system_ds/disk.i> vmid dsid
#    <hostA:system_ds/|hostB:system_ds/>
#   - hostX is the target host to deploy the VM
#   - system_ds is the path for the system datastore in the host
#   - vmid is the id of the VM
#   - dsid is the target datastore (0 is the system datastore)

SRC=$1
DST=$2

VMID=$3
DSID=$4

if [ -z "${ONE_LOCATION}" ]; then
    TMCOMMON=/var/lib/one/remotes/tm/tm_common.sh
else
    TMCOMMON=$ONE_LOCATION/var/remotes/tm/tm_common.sh
fi

. $TMCOMMON

#-------------------------------------------------------------------------------
# Return if moving a disk, we will move them when moving the whole system_ds
# directory for the VM
#-------------------------------------------------------------------------------
SRC=$(fix_dir_slashes $SRC)
DST=$(fix_dir_slashes $DST)

SRC_PATH=$(arg_path $SRC)
DST_PATH=$(arg_path $DST)

SRC_HOST=$(arg_host $SRC)
DST_HOST=$(arg_host $DST)

SRC_DIR=$(dirname $SRC_PATH)
DST_DIR=$(dirname $DST_PATH)

SRC_DS_DIR=$(dirname  $SRC_PATH)
SRC_VM_DIR=$(basename $SRC_PATH)

if [ "$(is_disk $DST_PATH)" -eq 1 ]; then
    exit 0
fi

#Do not try to move any files in PROLOG_MIGRATE_UNKNOWN (60) or
#PROLOG_MIGRATE_UNKNOWN_FAILURE (61)
LCM_STATE=$(lcm_state "$VMID")

if [[ "$LCM_STATE" =~ ^(60|61)$ ]]; then
    log "Not moving files from $SRC_HOST in FT mode"
    exit 0
fi

# preserve .monitor content (if any)
MONITOR_CMD=$(cat <<EOF
if [ -r '${SRC_DIR}/.monitor' ]; then
    cat '${SRC_DIR}/.monitor' 2>/dev/null || :
fi
EOF
)

MONITOR=$(ssh_monitor_and_log "$SRC_HOST" "$MONITOR_CMD" 'Get .monitor')

ssh_make_path $DST_HOST $DST_DIR
enable_local_monitoring $DST_HOST $DST_DIR "$MONITOR"

if [ "$SRC_DIR" == "$DST_DIR" ]; then
    RANDOM_FILE=$(ssh "$DST_HOST" "mktemp -p \"$DST_DIR\"")
    trap "ssh $DST_HOST \"rm -f $RANDOM_FILE\"" EXIT
    if ssh "$SRC_HOST" "test -f $RANDOM_FILE"; then
        log "Not moving $SRC to $DST, they are the same path"
        exit 0
    fi
fi

log "Moving $SRC to $DST"

ssh_exec_and_log "$DST_HOST" "rm -rf '$DST_PATH'" \
    "Error removing target path to prevent overwrite errors"

TAR_SSH=$(cat <<EOF
set -e -o pipefail

$TAR -C $SRC_DS_DIR --sparse -cf - $SRC_VM_DIR | $SSH $DST_HOST '$TAR -C $DST_DIR --sparse -xf -'
rm -rf $SRC_PATH
EOF
)

ssh_forward ssh_exec_and_log "$SRC_HOST" "$TAR_SSH" "Error copying disk directory to target host"

exit 0
